require(["config"],function(){
    require(["jquery","load"],function($){
        function register(){
            //页面初始化
            this.init();
        };
        $.extend(register.prototype,{
            init(){
                
                // 当用户名失去焦点的时候，判断验证用户输入
                $("#username").on("blur",this.verification);

                $(".submit").on("click",$.proxy(this.registerUser,this));
            },
            // 验证用户名
            verification(){
                const username = $("#username").val(),
                res = /^1(3|4|5|7|8)\d{9}$/; //手机号码正则验证
                if(!res.test(username)){
                    alert("请输入手机号码");
                    $("#username").val("");
                }
                return false;
            },
            // 注册
            registerUser(){
                const 
                username = $("#username").val(),
                passwrod1 = $("#passwrod1").val(),
                passwrod2 = $("#passwrod2").val();
                if(!username){
                    alert("手机号码不能为空！！！")
                    return false;
                }else{
                    if(passwrod1 != passwrod2){
                        alert("两次密码不一致");
                        $("#passwrod1").val("");
                        $("#passwrod2").val("");
                        return false;
                    }else{
                        const data = $(".register").serialize();
                        $.post("http://127.0.0.1/api/register.php", data,res =>{
                            if(res.res_body.status == 1){
                                location.href = "/html/login.html"
                            }else{
                                alert(res.res_body.message)
                            }
                        },"json");
                        return false;

                    }
                }

                
            }
            



        });
        new register();
    })
})